package com.cll.dao_ru_demo.controller;

import com.cll.dao_ru_demo.pojo.TUser;
import com.cll.dao_ru_demo.service.UserService;
import com.cll.dao_ru_demo.utils.Poi;
import excel.ExcelUtils;
import excel.ImportRspInfo;
import excel.POIConstant;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("list")
    public String list(){
        return "user/list";
    }

    @RequestMapping("intUser")
    public String intUser() throws Exception{
        //获取源文件
        Workbook wb = WorkbookFactory.create(new FileInputStream("D://user.xlsx"));
        //获取sheet0导入
        Sheet sheet = wb.getSheetAt(0);
        //生成VO实体
        ImportRspInfo<TUser> list = ExcelUtils.parseSheet(TUser.class,EvaluateVerifyBuilder2.getInstance(),sheet,1,0);

        boolean f = userService.addAll(list);
        if(f){
            return "user/list";
        }else {
            return "error";
        }
    }
    @RequestMapping("outUser")
    public void outUser(HttpServletResponse response) throws Exception {
        Poi p = new Poi();
        List list = userService.selectList();
        p.setList(list);
        p.setTitle("用户表");
        String[] a = {"编号","姓名","密码","性别","年龄","生日","地址"};
        p.setHeaders(a);
        Object[][] b = {{"uid", POIConstant.AUTO},
                        {"uname",POIConstant.AUTO},
                        {"pwd",POIConstant.AUTO},
                        {"sex",POIConstant.AUTO},
                        {"age",POIConstant.AUTO},
                        {"birthbay",POIConstant.AUTO},
                        {"address",POIConstant.AUTO}
        };
        p.setFields(b);
        Poi.getResponse(response);
        p.write(response);
    }
}
